package com.ysxsoft.wdg.ui.activity.my;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.ysxsoft.wdg.MyApplication;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.event.CommentEvent;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.ui.activity.MainActivity;
import com.ysxsoft.wdg.upversion.ApkUpdateService;
import com.ysxsoft.wdg.upversion.MyProgressDialog;
import com.ysxsoft.wdg.upversion.VersionBean;
import com.ysxsoft.wdg.util.SystemUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.widget.alertview.AlertViewFactory;
import com.ysxsoft.wdg.widget.alertview.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

import static com.ysxsoft.wdg.bean.event.CommentEvent.EventStatus.APK_update;
import static com.ysxsoft.wdg.bean.event.CommentEvent.EventStatus.MAIN_finish;

public class VersionActivity extends BaseActivity {

    public static void start(Context context){
        Intent intent = new Intent(context, VersionActivity.class);
        context.startActivity(intent);
    }
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_version;
    }

    @Override
    protected void initData() {
        initStatusBar(topView,false);
        titleContent.setText("版本更新");
        EventBus.getDefault().register(this);

        tvVersion.setText("当前版本"+SystemUtils.getVersion(mContext));
    }

    @Override
    protected void setListener() {

    }

    @OnClick({R.id.title_finish, R.id.btn_check})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_check:
                getVersion();
                break;
        }
    }


    private void getVersion() {
        final int curCode = SystemUtils.getVersionCode(this);
        mApiHelper.getVersion(new Observer<BaseBean<VersionBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<VersionBean> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    VersionBean dataBean = baseBean.getData();
                    ApkUpdateService.APK_UPDATE = dataBean.getPath();

                    int verCode = Integer.parseInt(dataBean.getVercode());
                    if (verCode > curCode){
                        handler.sendEmptyMessage(2);
                    }
                }else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }

    @Subscribe
    public void getEvent(CommentEvent event) {
        int code = event.getCode();
        if (code == APK_update) {
            if (isFront) {
                handler.sendEmptyMessage(0);
            }
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    /*************************   版本更新    *****************************/
    private MyProgressDialog progressDialog;
    private boolean isFront = false;

    @Override
    public void onResume() {
        super.onResume();
        isFront = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        isFront = false;
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    showProgressDialog();
                    break;
                case 2:
                    showUpView();
                    break;
            }
        }
    };

    private void showProgressDialog() {
        if (progressDialog != null) {
            progressDialog = null;
        }
        progressDialog = new MyProgressDialog();
        progressDialog.show(getSupportFragmentManager(), "progress");
    }

    private void showUpView() {
        AlertViewFactory.getInstance().getUpdateApkAlert(mContext, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                if (position == 0) {

                    new RxPermissions(VersionActivity.this)
                            .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            .subscribe(new Consumer<Boolean>() {
                                @Override
                                public void accept(Boolean aBoolean) throws Exception {
                                    ToastUtils.showToast("开始下载");
                                    Intent intent = new Intent(VersionActivity.this, ApkUpdateService.class);
                                    startService(intent);
                                }
                            });
                }
            }
        }).show();
    }
    /*************************   版本更新    *****************************/

}
